package com.huawei.petSystemdemo.ui.my;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.lifecycle.ViewModelProvider;

import com.huawei.petSystemdemo.BaseFragment;
import com.huawei.petSystemdemo.LoginActivity;
import com.huawei.petSystemdemo.R;
import com.huawei.petSystemdemo.ShoppingAdressActivity;
import com.huawei.petSystemdemo.databinding.FragmentMyBinding;
import com.huawei.petSystemdemo.ui.person.Personal_page;

public class MyFragment extends BaseFragment {
    private MyViewModel myViewModel;
    private FragmentMyBinding binding;
    private ImageView load;
    private TextView name;
    private TextView logout;
    private TextView integral;
    private ImageView setting;
    private ImageView wait_pay;
    private ImageView vip_img;
    private ImageView wait_fahuo;
    private ImageView fahuo;
    private ImageView wait_pingjia;
    private ImageView wait_tuikuan;
    private ImageView address;

    public View onCreateView(@NonNull LayoutInflater inflater,ViewGroup container, Bundle savedInstanceState) {

        myViewModel =  new ViewModelProvider(this).get(MyViewModel.class);

        binding = FragmentMyBinding.inflate(inflater, container, false);

        View root = binding.getRoot();
        load = root.findViewById(R.id.laod);
        name = root.findViewById(R.id.name);
        logout = root.findViewById(R.id.logout);
        integral = root.findViewById(R.id.my_integral);
        setting = root.findViewById(R.id.setting);
        wait_pay = root.findViewById(R.id.wait_pay);
        vip_img = root.findViewById(R.id.vip_img);
        wait_fahuo = root.findViewById(R.id.wait_fahuo);
        fahuo = root.findViewById(R.id.fahuo);
        wait_pingjia = root.findViewById(R.id.wait_pingjia);
        wait_tuikuan = root.findViewById(R.id.wait_tuikuan);
        address = root.findViewById(R.id.adress);


        //如果处于登录状态则自动设置名称
        if (getGlobal().isLogin())
        {
            name.setText(getGlobal().user.getUsername());
            logout.setVisibility(View.VISIBLE);//退出按钮可点
            integral.setText(getGlobal().user.getIntegral() + "");//用户积分
        }

        //点击后跳转到登录页面
        // 第一次登录传递信息
        load.setOnClickListener(v -> {
                if(getGlobal().isLogin()){//已经登录就不用再点头像登录了
                    return;
                }
                Intent intent = new Intent(getContext(), LoginActivity.class);
                startActivityForResult(intent, 1);
                //startActivity(intent);
            });
        //退出
        logout.setOnClickListener(v->{
            //global.logout();
            if(getGlobal().isLogin()){
                logout.setVisibility(View.GONE);
                getGlobal().user = null;
            }
        });


        //设置
        setting.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(getGlobal().isLogin()){//只有处于登录状态时，才能点右上角的个人信息修改
                    Intent intent = new Intent(getContext(), Personal_page.class);
                    intent.putExtra("username",getGlobal().user.getUsername());
                    System.out.println(getGlobal().user.getUsername());
                    startActivity(intent);
                }

            }
        });

        wait_pay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getContext(), com.huawei.petSystemdemo.ui.mOrder.OrderActivity.class);
                intent.putExtra("name", 0);
                startActivity(intent);
            }
        });

        wait_fahuo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getContext(), com.huawei.petSystemdemo.ui.mOrder.OrderActivity.class);
                intent.putExtra("name", 1);
                startActivity(intent);
            }
        });

        fahuo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getContext(), com.huawei.petSystemdemo.ui.mOrder.OrderActivity.class);
                intent.putExtra("name", 2);
                startActivity(intent);
            }
        });

        wait_pingjia.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getContext(), com.huawei.petSystemdemo.ui.mOrder.OrderActivity.class);
                intent.putExtra("name", 3);
                startActivity(intent);
            }
        });

        wait_tuikuan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getContext(), com.huawei.petSystemdemo.ui.mOrder.OrderActivity.class);
                intent.putExtra("name", 4);
                startActivity(intent);
            }
        });

        vip_img.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getContext(), VipActivity.class);
                startActivity(intent);
            }
        });

        address.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getContext(), ShoppingAdressActivity.class);
                startActivity(intent);
            }
        });

        return root;
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == 10) {
            if (requestCode == 1) {
                //String three = data.getExtras();
                //String three = data.getExtras().getString("name");
                //设置结果显示框的显示数值
                name.setText(getGlobal().user.getUsername());
                integral.setText(getGlobal().user.getIntegral() + "");
            }
        }
    }

}